package com.example.mediaplayerpractice_2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private VideoView videoView;
    private Button buttonPlay;
    private Button buttonPause;
    private Button buttonStop;
    //动态申请权限；
    private static String[] PERMISSIONS = {
            "android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.READ_EXTERNAL_STORAGE"
    };
    static boolean FLAG_PERMISSIONS = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUi();
        checkPermission();
        if(FLAG_PERMISSIONS){
            initVideo();
        }
    }

    private void initVideo() {
        //读取在raw中的视频文件
        videoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/raw/video"));
    }

    private void checkPermission() {
        //进行权限查询，这个和之前的ContentProvider中的权限申请，很像，可参考！
        for(String permission : PERMISSIONS){
            if(ContextCompat.checkSelfPermission(MainActivity.this,permission) != PackageManager.PERMISSION_GRANTED ){
                //审查权限
                ActivityCompat.requestPermissions(this,PERMISSIONS,1);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 1:
                for (int grantResult : grantResults){
                    if(grantResult != PackageManager.PERMISSION_GRANTED){
                        FLAG_PERMISSIONS = false;
                        Toast.makeText(getApplicationContext(),"Permisson is denied!",Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
                FLAG_PERMISSIONS = true;
                break;
        }
    }
    private void initUi() {
        videoView = findViewById(R.id.image_view);
        buttonPlay = findViewById(R.id.button_play);
        buttonPause = findViewById(R.id.button_pause);
        buttonStop = findViewById(R.id.button_stop);
        buttonStop.setOnClickListener(this);
        buttonPause.setOnClickListener(this);
        buttonPlay.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.button_play:
                if(!videoView.isPlaying()){
                    videoView.start();
                }
                break;
            case R.id.button_pause:
                if(videoView.isPlaying()){
                    videoView.pause();
                }
                break;
            case R.id.button_stop:
                if(videoView.isPlaying()){
                    videoView.resume();//重播
                }
                break;

            default:
                throw new IllegalStateException("Unexpected value: " + v.getId());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(videoView!=null){
            videoView.suspend();
        }
    }
}